package com.readbook.action;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.readbook.service.WdsjService;
import com.readbook.service.WxsycService;
import com.readbook.util.ExcelUtils;
import com.readbook.util.ExportExcel;
import com.readbook.util.IDCardUtil;
import com.readbook.util.SecCheck;
import net.sf.jxls.exception.ParsePropertyException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/wxsyc")
public class wxsyc {
    @Resource
    private WxsycService wxsycService;

    @Resource
    private WdsjService wdsjService;

    /**
     * @author 牟明艳
     * @comments 原创广场
     * @time 2019年9月5日11:26:46
     */
    @RequestMapping("getycgc.html")
    @ResponseBody
    public Map<String, Object> getycgc(HttpServletRequest request, HttpServletResponse response) {
        String usid = "";
        Map<String, Object> jstjmap = new HashMap<String, Object>();
        if (request.getParameter("usid") != null) {
            usid = request.getParameter("usid");

        } else {
            jstjmap.put("bstate", "2");
        }
        String keyword = request.getParameter("keyword");
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int startn = page * limit - limit;//起始
        int endn = page * limit;//结束
        //原创广场
        jstjmap.put("start", startn);
        jstjmap.put("end", endn);
        jstjmap.put("usid", usid);
        jstjmap.put("keyword", keyword);

        List<Map<String, Object>> list1 = wxsycService.findycgcPage(jstjmap);
        int totalnum = wxsycService.countycgcPage(jstjmap);

        double c = (double) totalnum / limit;
        int totalpage = (int) Math.ceil(c);

        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("list", list1);
        rmap.put("totalpage", totalpage);
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 我的推荐
     * @time 2019年9月5日12:01:37
     */
    @RequestMapping("getwdtj.html")
    @ResponseBody
    public Map<String, Object> getwdtj(HttpServletRequest request, HttpServletResponse response) {
        String usid = "";
        if (request.getParameter("usid") != null) {
            usid = request.getParameter("usid");
        }
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int startn = page * limit - limit;//起始
        int endn = page * limit;//结束
        String keyword = request.getParameter("keyword");
        //原创广场
        Map<String, Object> jstjmap = new HashMap<String, Object>();
        jstjmap.put("start", startn);
        jstjmap.put("end", endn);
        jstjmap.put("usid", usid);
        jstjmap.put("keyword", keyword);
        List<Map<String, Object>> list1 = wxsycService.findwdtjPage(jstjmap);
        int totalnum = wxsycService.countwdtjPage(jstjmap);

        double c = (double) totalnum / limit;
        int totalpage = (int) Math.ceil(c);

        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("list", list1);
        rmap.put("totalpage", totalpage);
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 查询学生原创书籍的评论
     * @time 2019年9月7日14:47:29
     */
    @RequestMapping("getxsycPl.html")
    @ResponseBody
    public Map<String, Object> getxsycPl(HttpServletRequest request, HttpServletResponse response) {
        String ycid = request.getParameter("ycid");
        Map<String, Object> gmap = new HashMap<>();
        gmap.put("ycid", ycid);
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int startn = page * limit - limit;//起始
        int endn = page * limit;//结束
        gmap.put("start", startn);
        gmap.put("end", endn);
        List<Map<String, Object>> listmaps = wxsycService.getxsycPl(gmap);
        int totalnum = wxsycService.countxsycPl(gmap);
        double c = (double) totalnum / limit;
        int totalpage = (int) Math.ceil(c);

        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("list", listmaps);
        rmap.put("totalpage", totalpage);
        return rmap;
    }

    /**
     * @author 牟明艳
     * @comments 学生原创
     * @time 2019年9月5日12:41:05
     */
    @RequestMapping("addBookxsyc.html")
    @ResponseBody
    public Map<String, Object> addBookxsyc(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> smap = new HashMap<String, Object>();
        String usid = "";
        if (request.getParameter("usid") != null) {
            usid = request.getParameter("usid");
        }
        String sname = "";
        if (request.getParameter("username") != null) {
            sname = request.getParameter("username");
        }
        String nj = "";
        if (request.getParameter("nj") != null) {
            nj = request.getParameter("nj");
        }
        String bj = "";
        if (request.getParameter("bj") != null) {
            bj = request.getParameter("bj");
        }
        String zpmc = "";
        if (request.getParameter("zpmc") != null) {
            zpmc = request.getParameter("zpmc");
        }
        String content = "";
        if (request.getParameter("content") != null) {
            content = request.getParameter("content");
        }
        String bstate = "";
        if (request.getParameter("bstate") != null) {
            bstate = request.getParameter("bstate");
        }
        String ycid = UUID.randomUUID().toString().replaceAll("-", "");
        smap.put("ycid", ycid);
        smap.put("usid", usid);
        smap.put("sname", sname);
        smap.put("nj", nj);
        smap.put("bj", bj);
        smap.put("zpmc", zpmc);
        smap.put("content", content);
        smap.put("bstate", bstate);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        smap.put("addtime", df.format(new Date()));
        Map<String, Object> remap = new HashMap<String, Object>();

        //检测内容是否违规
        String pjson= JSON.toJSONString(smap);
        remap= SecCheck.msgSecCheck(pjson);
        if(remap.get("result").toString().equals("false")){
            return remap;
        }

        int i = wxsycService.addBookxsyc(smap);
        if (i > 0) {

            String upurls = "";
            if (request.getParameter("upurls") != null) {
                upurls = request.getParameter("upurls");
            }
            List<String> listr = (List<String>) JSONArray.parse(upurls);
            for (int m = 0; m < listr.size(); m++) {
                String picurl = listr.get(m);
                String code = "bookxsyc";
                String codename = "xszp";

                Map<String, Object> imap = new HashMap<String, Object>();
                imap.put("picurl", picurl);
                imap.put("code", code);
                imap.put("codename", codename);
                imap.put("pid", ycid);
                imap.put("xh", m);
                int k = wdsjService.savePicture(imap);
                if (k <= 0) {
                    remap.put("msg", "提交失败!");
                    remap.put("result", true);
                }
            }

            remap.put("msg", "提交成功!");
            remap.put("result", true);
        } else {
            remap.put("msg", "提交失败!");
            remap.put("result", false);
        }
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments 家长推荐
     * @time 2019年9月5日12:41:13
     */
    @RequestMapping("addBookJztj.html")
    @ResponseBody
    public Map<String, Object> addBookJztj(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> smap = new HashMap<String, Object>();
        String bookname = "";
        if (request.getParameter("bookname") != null) {
            bookname = request.getParameter("bookname");
        }
        String classid = "";
        if (request.getParameter("classid") != null) {
            classid = request.getParameter("classid");
        }
        String fm = "";
        if (request.getParameter("fm") != null) {
            fm = request.getParameter("fm");
        }
        String author = "";
        if (request.getParameter("author") != null) {
            author = request.getParameter("author");
        }

        String totalpage = "";
        if (request.getParameter("totalpage") != null) {
            totalpage = request.getParameter("totalpage");
        }

        String nj = "";
        if (request.getParameter("nj") != null) {
            nj = request.getParameter("nj");
        }
        String minnum = "";
        if (request.getParameter("minnum") != null) {
            minnum = request.getParameter("minnum");
        }
        String words = "";
        if (request.getParameter("words") != null) {
            words = request.getParameter("words");
        }


        String reason = "";
        if (request.getParameter("reason") != null) {
            reason = request.getParameter("reason");
        }
        String recommendor = "";
        if (request.getParameter("recommendor") != null) {
            recommendor = request.getParameter("recommendor");
        }
        String bookjj = "";
        if (request.getParameter("bookjj") != null) {
            bookjj = request.getParameter("bookjj");
        }
        String recommendorid = "";
        if (request.getParameter("recommendorid") != null) {
            recommendorid = request.getParameter("recommendorid");
        }
        String authorjj = "";
        if (request.getParameter("authorjj") != null) {
            authorjj = request.getParameter("authorjj");
        }
        String bookxx = "";
        if (request.getParameter("bookxx") != null) {
            bookxx = request.getParameter("bookxx");
        }
        String tjly = "1";
        if (request.getParameter("tjly") != null) {
            tjly = request.getParameter("tjly");
        }
        String bid = UUID.randomUUID().toString().replaceAll("-", "");
        smap.put("bid", bid);
        smap.put("bookname", bookname);
        smap.put("classid", classid);
        smap.put("fm", fm);
        smap.put("author", author);
        smap.put("totalpage", totalpage);
        smap.put("nj", nj);
        smap.put("minnum", minnum);
        smap.put("words", words);
        smap.put("reason", reason);
        smap.put("recommendor", recommendor);
        smap.put("bookjj", bookjj);
        smap.put("recommendorid", recommendorid);
        smap.put("authorjj", authorjj);
        smap.put("bookxx", bookxx);
        smap.put("sh", "0");
        //推荐来源根据登录账号来
        smap.put("tjly", tjly);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        smap.put("addtime", df.format(new Date()));

        //先判断是否有不符合的法律的数据
        String pjson= JSON.toJSONString(smap);
        Map<String,Object>pd=new HashMap<String,Object>();
        pd= SecCheck.msgSecCheck(pjson);
        if(pd.get("result").toString().equals("false")){
            return pd;
        }

        int i = wxsycService.addBookJztj(smap);

        Map<String, Object> remap = new HashMap<String, Object>();
        if (i > 0) {
            remap.put("msg", "保存成功!");
            remap.put("result", true);
        } else {
            remap.put("msg", "保存失败!");
            remap.put("result", false);
        }
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments 查询书籍类别
     * @time 2019年9月5日19:02:26
     */
    @RequestMapping("getBookClass.html")
    @ResponseBody
    public List<Map<String, Object>> getBookClass(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> list1 = wxsycService.getBookClass();
        return list1;
    }


    /**
     * @author 牟明艳
     * @comments 查询书籍详情
     * @time 2019年9月5日19:02:26
     */
    @RequestMapping("getxsycDetail.html")
    @ResponseBody
    public Map<String, Object> getxsycDetail(HttpServletRequest request, HttpServletResponse response) {
        String ycid = request.getParameter("ycid");
        Map<String, Object> gmap = new HashMap<>();
        gmap.put("ycid", ycid);
        Map<String, Object> maps = wxsycService.getxsycDetail(gmap);
        return maps;
    }


    /**
     * @author 牟明艳
     * @comments 学生原创修改
     * @time 2019年9月5日12:41:05
     */
    @RequestMapping("updateBookxsyc.html")
    @ResponseBody
    public Map<String, Object> updateBookxsyc(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> smap = new HashMap<String, Object>();
        String ycid = "";
        if (request.getParameter("ycid") != null) {
            ycid = request.getParameter("ycid");
        }
        String zpmc = "";
        if (request.getParameter("zpmc") != null) {
            zpmc = request.getParameter("zpmc");
        }
        String content = "";
        if (request.getParameter("content") != null) {
            content = request.getParameter("content");
        }
        String bstate = "";
        if (request.getParameter("bstate") != null) {
            bstate = request.getParameter("bstate");
        }

        smap.put("ycid", ycid);
        smap.put("zpmc", zpmc);
        smap.put("content", content);
        smap.put("bstate", bstate);


        Map<String, Object> remap = new HashMap<String, Object>();
        //检测内容是否违规
        String pjson= JSON.toJSONString(smap);
        remap= SecCheck.msgSecCheck(pjson);
        if(remap.get("result").toString().equals("false")){
            return remap;
        }


        int i = wxsycService.updateBookxsyc(smap);
        if (i > 0) {
            wxsycService.deletePicture(smap);
            String upurls = "";
            if (request.getParameter("upurls") != null) {
                upurls = request.getParameter("upurls");
            }
            List<String> listr = (List<String>) JSONArray.parse(upurls);
            for (int m = 0; m < listr.size(); m++) {
                String picurl = listr.get(m);
                String code = "bookxsyc";
                String codename = "xszp";

                Map<String, Object> imap = new HashMap<String, Object>();
                imap.put("picurl", picurl);
                imap.put("code", code);
                imap.put("codename", codename);
                imap.put("pid", ycid);
                imap.put("xh", m);
                int k = wdsjService.savePicture(imap);
                if (k <= 0) {
                    remap.put("msg", "提交失败!");
                    remap.put("result", true);
                }
            }

            remap.put("msg", "发布成功!");
            remap.put("result", true);
        } else {
            remap.put("msg", "发布失败!");
            remap.put("result", false);
        }
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments 学生原创评论
     * @time 2019年9月7日15:29:07
     */
    @RequestMapping("addBookxsycpl.html")
    @ResponseBody
    public Map<String, Object> addBookxsycpl(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> smap = new HashMap<String, Object>();
        String ycid = "";
        if (request.getParameter("ycid") != null) {
            ycid = request.getParameter("ycid");
        }
        String usid = "";
        if (request.getParameter("usid") != null) {
            usid = request.getParameter("usid");
        }
        String sname = "";
        if (request.getParameter("sname") != null) {
            sname = request.getParameter("sname");
        }
        String nj = "";
        if (request.getParameter("nj") != null) {
            nj = request.getParameter("nj");
        }
        String bj = "";
        if (request.getParameter("bj") != null) {
            bj = request.getParameter("bj");
        }
        String plcontent = "";
        if (request.getParameter("plcontent") != null) {
            plcontent = request.getParameter("plcontent");
        }
        String pstar = "";
        if (request.getParameter("pstar") != null) {
            pstar = request.getParameter("pstar");
        }

        smap.put("ycid", ycid);
        smap.put("usid", usid);
        smap.put("sname", sname);
        smap.put("nj", nj);
        smap.put("bj", bj);
        smap.put("plcontent", plcontent);
        smap.put("pstar", pstar);
        smap.put("jf", Integer.parseInt(pstar) * 0.5);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        smap.put("addtime", df.format(new Date()));

        //检测内容是否违规
        String pjson= JSON.toJSONString(smap);
        Map<String, Object> remap = new HashMap<String, Object>();
        remap= SecCheck.msgSecCheck(pjson);
        if(remap.get("result").toString().equals("false")){
            return remap;
        }

        int i = wxsycService.addBookxsycpl(smap);
        if (i > 0) {
            //根据原创ID查询创作人
            Map<String, Object> ymaps = wxsycService.getxsycDetailByid(smap);
            //记录至积分记录文件
            Map<String, Object> jfmap = new HashMap<String, Object>();
            String jfid = UUID.randomUUID().toString().replaceAll("-", "");
            jfmap.put("jfid", jfid);
            jfmap.put("lyid", ycid);
            jfmap.put("jfly", "bookxsycpl");//积分来源 学生原创评论
            jfmap.put("addtime", df.format(new Date()));
            jfmap.put("usid", ymaps.get("USID"));
            jfmap.put("username", ymaps.get("SNAME"));
            jfmap.put("jfnum", Integer.parseInt(pstar) * 0.5);
            wdsjService.savejfjl(jfmap);
            remap.put("msg", "保存成功!");
            remap.put("result", true);
        } else {
            remap.put("msg", "保存失败!");
            remap.put("result", false);
        }
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments 删除原创
     * @time 2019年9月5日12:41:13
     */
    @RequestMapping("deleteXsycByycid.html")
    @ResponseBody
    public Map<String, Object> deleteXsycByycid(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> smap = new HashMap<String, Object>();
        String ycid = "";
        if (request.getParameter("ycid") != null) {
            ycid = request.getParameter("ycid");
        }
        smap.put("ycid", ycid);
        int i = wxsycService.deleteXsycByycid(smap);
        Map<String, Object> remap = new HashMap<String, Object>();
        if (i > 0) {
            remap.put("msg", "删除成功!");
            remap.put("result", true);
        } else {
            remap.put("msg", "删除失败!");
            remap.put("result", false);
        }
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments  学生导入
     * @time
     */
    @RequestMapping("importNewStudent.html")
    @ResponseBody
    public void importNewStudent(HttpServletRequest request, HttpServletResponse response) throws ParsePropertyException, IOException, InvalidFormatException {
        List<Map<String, Object>> list1= ExcelUtils.getLocationExceldatas();
        List<Map<String, Object>> errlist=new ArrayList<Map<String, Object>>();
        for(int i=0;i<list1.size();i++){
            Map<String,Object>maps=new HashMap<>();
            maps=list1.get(i);
            if(maps.get("身份证号")!=null && maps.get("姓名")!=null && maps.get("班主任")!=null && maps.get("班级")!=null){
                String sfzh=maps.get("身份证号").toString().trim().replaceAll("'","");
                String yz= IDCardUtil.IDCardValidate(sfzh);

                //查询是否已经存在，如果存在 则跳过
                Map<String,Object>map1=new HashMap<>();
                map1.put("XH",maps.get("序号"));
                map1.put("ID",maps.get("身份证号"));
                map1.put("SFZH",sfzh);
                map1.put("NAME",maps.get("姓名"));
                map1.put("BZR",maps.get("班主任"));
                map1.put("BJ",maps.get("班级"));
                map1.put("yz",yz);
                if(yz.equals("1")){
                    List<Map<String, Object>> list2=wxsycService.listNewStudent(map1);
                    if(list2.size()<=0){
                        wxsycService.addNewStudent(map1);
                    }
                }else{
                    errlist.add(map1);
                }
            }
        }
        Map<String,Object>rmaps=new HashMap<>();
        rmaps.put("lists",errlist);
        ExportExcel.download("newstudent.xlsx",rmaps,request,response);
    }



    /**
     * @author 牟明艳
     * @comments  根据身份证查询学生数据
     * @time
     */
    @RequestMapping("listNewStudent.html")
    @ResponseBody
    public Map<String, Object> listNewStudent(HttpServletRequest request, HttpServletResponse response) {
        String SFZH=request.getParameter("sfzh");
        Map<String,Object>map1=new HashMap<>();
        map1.put("SFZH",SFZH.trim());
        List<Map<String, Object>> list2=wxsycService.listNewStudent(map1);
        Map<String, Object> remap = new HashMap<String, Object>();
        if(list2.size()>0){
            remap=list2.get(0);
            remap.put("result", true);
        }else{
            remap.put("msg", "对不起，没有该数据，请联系学校负责人!");
            remap.put("result", false);
        }
        return remap;
    }



}
